package com.crm.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.crm.entity.Parts;
import com.crm.entity.StockEntity;
import com.crm.util.ApplicationContext;

public class StockDAO {
	protected final Log logger = LogFactory.getLog(getClass());

	public  void insert(StockEntity ss)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(ss);
			tx.commit();
			return ;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}	
	public  void insertStockList(List<StockEntity> ss)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			for(StockEntity se:ss){
				session.saveOrUpdate(se);
			}
			tx.commit();
			return ;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}	
	public  List<StockEntity> findAll(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try { 
			List<StockEntity> custObjs=new ArrayList<StockEntity>();
			List objs=null;
			Query query=session.createQuery("from StockEntity where business_id=:busid");
			query.setInteger("busid", busid);
			objs=query.list();
			for(Object o:objs){
				custObjs.add((StockEntity)o);
			}
			return custObjs;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  void insertStock(StockEntity s)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction(); 
			session.save(s);
			tx.commit();
			return ;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}	
	public  void update(int id,int qty,int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			Query query=session.createQuery("update StockEntity set qty=:qty where id=:id and business_id=:busid");
			query.setInteger("id", id);
			query.setInteger("qty", qty);		
			query.setInteger("busid", busid);
			query.executeUpdate();
			tx.commit();
			return ;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public void deleteStock(List<Parts> partsList, int busid) throws HibernateException, Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			for (Parts p:partsList) {
				Query query = session
						.createQuery("update StockEntity set qty=qty-"+p.getQty()+"where id=:id and business_id=:busid");
				query.setInteger("id", p.getPartid());
				//query.setInteger("qty", p.getQty());
				query.setInteger("busid", busid);
				query.executeUpdate();
			}
			tx.commit();
			return ;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public StockEntity findByid(int id, int busid) throws HibernateException, Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try { 
			Query query=session.createQuery("from StockEntity where business_id=:busid and id=:id");
			query.setInteger("busid", busid);
			query.setInteger("id", id);
			StockEntity se=(StockEntity)query.uniqueResult();
			return se;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public StockEntity findByPartId(int partId, int busid) throws HibernateException, Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try { 
			Query query=session.createQuery("from StockEntity where business_id=:busid and part_no=:partId");
			query.setInteger("busid", busid);
			query.setInteger("partId", partId);
			StockEntity se=(StockEntity)query.uniqueResult();
			return se;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public List<StockEntity> findStock(String partNumbers, int businessId) throws HibernateException, Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try { 
			Query query=session.createQuery("from StockEntity s where s.partNo in(select p.partid from Parts p where p.prtnum in("+partNumbers+")) and business_id=:busid");
			query.setInteger("busid", businessId);
			List<StockEntity> se=(List<StockEntity>)query.list();
			return se;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
		
	}

}
